package sv;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import sv.WelcomeTable.LimitedDocument;


public class SolverChoose extends JFrame implements ActionListener{
	private JCheckBox[] cb = new JCheckBox[3];
	private JCheckBox vis;
	private WelcomeWindow wWindow;
	private JTextField ritt;
	
	public SolverChoose(WelcomeWindow ww) {
		wWindow=ww;
		
		setTitle("Scegli");
		setSize(200,250);
		setLayout(new GridLayout(6,1));
		
		setBackground(Color.white);
		
		vis = new JCheckBox("Output", true);
		JPanel p = new JPanel();
		p.setBorder(new LineBorder(Color.black,1));
		p.add(vis);
		p.setBackground(Color.white);
		add(p);
		
		for(int i=0;i<3;i++){
			//JLabel lab = new JLabel("Solver"+(i+1));
			cb[i] = new JCheckBox("Solver"+(i+1),true);
			p = new JPanel();
			p.add(cb[i]);
			p.setBackground(Color.white);
			add(p);
		}
		
		ritt =new JTextField("100");
		ritt.setHorizontalAlignment(JTextField.CENTER);
		ritt.setBorder(new TitledBorder("Ritardo"));
		ritt.setDocument ( new LimitedDocument ());
		ritt.setText("100");
		add(ritt);
		
		JButton a = new JButton("VAI");
		a.addActionListener(this);
		add(a);
		
		setResizable(false);
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	public void actionPerformed (ActionEvent e){
		int count=0;
		
		for(int i=0;i<3;i++){
			if(cb[i].isSelected())
				count++;
		}
		if(count==0)
			JOptionPane.showMessageDialog(null, "Deci selezionare almeno un solver");
		else{
			this.dispose();
			int[] solvers = new int[count];
			count=0;
			for(int i=0;i<3;i++){
				if(cb[i].isSelected())
					solvers[count++]=i;
			}
			int rit;
			try{
				rit=Integer.valueOf(ritt.getText());
				if(rit>60000){
					rit=60000;
				}
			}
			catch(java.lang.NumberFormatException ne){
				rit=100;
			}
			SolverManager sm = new SolverManager(wWindow,solvers,vis.isSelected(),rit);
		}
	}
	
	class LimitedDocument extends PlainDocument{
		private int limit=5;
		
		public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
			if(getLength()<limit){
				super.insertString(offs,str,a);
			}
		}
	}
}

class TestSC{
	public static void main(String[] args){
		SolverChoose sc = new SolverChoose(null);
	}
}

